package ubo.master1.jeupoker.gui;

import java.awt.FlowLayout;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ControlPanel extends JPanel {

    /** The Check button. */
    private final JButton checkButton;
    /** The Call button. */
    private final JButton callButton;
    /** The Bet button. */
    private final JButton betButton;
    /** The Raise button. */
    private final JButton raiseButton;
    /** The Fold button. */
    private final JButton foldButton;
    /** The Continue button. */
    private final JButton continueButton;

    public enum Action {

        /** Posting the small blind. */
        SMALL_BLIND("Small Blind", "posts the small blind"),
        /** Posting the big blind. */
        BIG_BLIND("Big Blind", "posts the big blind"),
        /** Checking. */
        CHECK("Check", "checks"),
        /** Calling a bet. */
        CALL("Call", "calls"),
        /** Place an initial bet. */
        BET("Bet", "bets"),
        /** Raising the current bet. */
        RAISE("Raise", "raises"),
        /** Folding. */
        FOLD("Fold", "folds"),
        /** Continuing the game. */
        CONTINUE("Continue", "continues"),;
        /** The name. */
        private final String name;
        /** The verb. */
        private final String verb;

        /**
         * Constructor.
         * 
         * @param name
         *            The name.
         */
        Action(String name, String verb) {
            this.name = name;
            this.verb = verb;
        }

        /**
         * Returns the name.
         * 
         * @return The name.
         */
        public String getName() {
            return name;
        }

        /**
         * Returns the verb form of this action.
         * 
         * @return The verb.
         */
        public String getVerb() {
            return verb;
        }

        /*
         * (non-Javadoc)
         * @see java.lang.Enum#toString()
         */
        @Override
        public String toString() {
            return name;
        }
    }
    /** The selected action. */
    private Action action;

    /**
     * Constructor.
     * 
     * @param main
     *            The main frame.
     */
    public ControlPanel() {
        //setBackground(UIConfiguration.TABLE_COLOR.WHITE);
        this.setOpaque(false);
        setLayout(new FlowLayout());
        continueButton = createActionButton(Action.CONTINUE);
        checkButton = createActionButton(Action.CHECK);
        callButton = createActionButton(Action.CALL);
        betButton = createActionButton(Action.BET);
        raiseButton = createActionButton(Action.RAISE);
        foldButton = createActionButton(Action.FOLD);

        add(continueButton);
        //Pour affichet les action possible 
        add(raiseButton);
        add(checkButton);
        add(callButton);
        add(raiseButton);
        add(foldButton);

    }

    /**
     * Waits for the user to click an action button and returns the selected
     * action.
     * 
     * @param allowedActions
     *            The allowed actions.
     * 
     * @return The selected action.
     */
    public Action getUserInput(final Set<Action> allowedActions) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // Show the buttons for the allowed actions.
                removeAll();
                if (allowedActions.contains(Action.CONTINUE)) {
                    add(continueButton);
                } else {
                    if (allowedActions.contains(Action.CHECK)) {
                        add(checkButton);
                    }
                    if (allowedActions.contains(Action.CALL)) {
                        add(callButton);
                    }
                    if (allowedActions.contains(Action.BET)) {
                        add(betButton);
                    }
                    if (allowedActions.contains(Action.RAISE)) {
                        add(raiseButton);
                    }
                    if (allowedActions.contains(Action.FOLD)) {
                        add(foldButton);
                    }
                }
                validate();
                repaint();
            }
        });

        return action;
    }

    /**
     * Creates an action button.
     * 
     * @param action
     *            The action.
     * 
     * @return The button.
     */
    private JButton createActionButton(Action action) {
        String label = action.getName();
        JButton button = new JButton(label);
        button.setMnemonic(label.charAt(0));
        button.setSize(100, 30);
        return button;
    }
}
